<template>
	<Echart
		id="leftCenter1"
		:options="options"
		class="left_center_inner"
		ref="echarts"
	/>
</template>

<script>
import { graphic } from "echarts";
import { getgraph } from "@/api/modules/home.js";

export default {
	props: {
		id: {
			type: String,
			required: true,
		},
		cname: {
			type: String,
			required: true,
		},
	},
	computed: {
		isCollapse() {
			return this.$store.state.isCollapse;
		},
	},
	watch: {
		isCollapse(oldVal, newVal) {
			if (oldVal !== newVal) {
				this.$refs.echarts.resize();
			}
		},
	},
	data() {
		return {
			options: {},
		};
	},
	mounted() {
		this.getData();
	},
	beforeDestroy() {},
	methods: {
		getData() {
			getgraph({ devId: this.id, sampleName: "ClusterCurrent" }).then((res) => {
				if (res.code == 200) {
					let xData = [];
					let ygList = [];
					// let wgList = []
					let data = res.data;

					for (let index = 0; index < data.length; index++) {
						let element = data[index];
						xData.push(element.date);
						ygList.push(element.graphHisDTO ? element.graphHisDTO.value : "");
					}

					this.$nextTick(() => {
						this.init(xData, ygList, this.cname);
					});
				} else {
					this.$Message({
						text: res.msg,
						type: "warning",
					});
				}
			});
			// scoInfo({ plieID: this.info.id }).then((res) => {

			//   if (res.code == 200) {
			//     let xData = []
			//     let ygList = []
			//     // let wgList = []
			//     let maxygList = []
			//     let maxwgList = []
			//     let data = res.data

			//     for (let index = 0; index < data.length; index++) {
			//       let element = data[index];
			//       xData.push(element.date)
			//       ygList.push(element.sampleHisDTO.length > 0 ? element.sampleHisDTO[0]['value34'] : '') //实时有效功率

			//     }
			//     this.$nextTick(() => {
			//       this.init(xData, ygList, this.info.cname)
			//     });
			//   } else {
			//     this.$Message({
			//       text: res.msg,
			//       type: "warning",
			//     });
			//   }
			// })
		},
		init(xData, yData, cname) {
			this.options = {
				legend: {
					x: "center", //可设定图例在左、右、居中
					y: "10px",
					data: ["电流"],
					textStyle: {
						fontSize: 12,
						color: "#F1F1F3",
					},
				},
				xAxis: {
					type: "category",
					data: xData,
					boundaryGap: false, // 不留白，从原点开始
					splitLine: {
						show: true,
						lineStyle: {
							color: "rgba(31,99,163,.2)",
						},
					},
					axisLine: {
						// show:false,
						lineStyle: {
							color: "rgba(31,99,163,.1)",
						},
					},
					axisLabel: {
						color: "#7EB7FD",
						fontWeight: "500",
					},
				},
				yAxis: {
					name: "单位 A",
					type: "value",
					nameTextStyle: {
						color: "#fff",
						fontSize: 12,
					},
					splitLine: {
						show: true,
						lineStyle: {
							color: "rgba(31,99,163,.2)",
						},
					},
					axisLine: {
						lineStyle: {
							color: "rgba(31,99,163,.1)",
						},
					},
					axisLabel: {
						color: "#7EB7FD",
						fontWeight: "500",
					},
				},
				tooltip: {
					trigger: "axis",
					backgroundColor: "rgba(0,0,0,.6)",
					borderColor: "rgba(147, 235, 248, .8)",
					textStyle: {
						color: "#FFF",
					},
					formatter: function (params) {
						var result = params[0].name + "<br/>";
						params.forEach(function (item) {
							result += cname + ": " + item.value + "A<br/>";
						});
						return result;
					},
				},
				grid: {
					//布局
					show: true,
					left: "30px",
					right: "30px",
					bottom: "10px",
					top: "48px",
					containLabel: true,
					borderColor: "#1F63A3",
				},
				series: [
					// {
					//   data: yData,
					//   type: "line",
					//   symbol: "circle",
					//            //           //   itemStyle: {
					//     color: "#fd754b",
					//   },
					//   name: "有功功率",
					//   areaStyle: {
					//     //右，下，左，上
					//     color: new graphic.LinearGradient(
					//       0,
					//       0,
					//       0,
					//       1,
					//       [
					//         {
					//           offset: 0,
					//           color: "rgba(252,144,16,.7)",
					//         },
					//         {
					//           offset: 1,
					//           color: "rgba(252,144,16,.0)",
					//         },
					//       ],
					//       false
					//     ),
					//   }
					// },
					{
						data: yData,
						type: "line",
						symbol: "none",
						//点样式
						itemStyle: {
							color: "#5083FF",
						},
						name: "SOC",
						areaStyle: {
							//右，下，左，上
							borderRadius: [2, 2, 0, 0], //柱体圆角
							color: new graphic.LinearGradient(
								0,
								0,
								0,
								1,
								[
									{
										offset: 0,
										color: "rgba(24,210,181,.35)",
									},
									{
										offset: 1,
										color: "rgba(30,231,231,1)",
									},
								],
								false
							),
						},
					},
				],
			};
		},
	},
};
</script>
<style lang="scss" scoped></style>
